package cn.cdutetc.library.controller;

import cn.cdutetc.library.entity.pojo.BookCategory;
import cn.cdutetc.library.common.ApiResponse;
import cn.cdutetc.library.entity.dto.BookCategoryDTO;
import cn.cdutetc.library.service.BookCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 图书分类控制器
 */
@RestController
@RequestMapping("/api/book-categories")
@Tag(name = "图书分类管理", description = "图书分类管理相关接口")
public class BookCategoryController {

    @Autowired
    private BookCategoryService bookCategoryService;

    @PostMapping
    @Operation(summary = "创建图书分类", description = "创建新的图书分类")
    public ApiResponse<Integer> createCategory(@RequestBody @Validated BookCategoryDTO categoryDTO) {
        Integer categoryId = bookCategoryService.createCategory(categoryDTO);
        return ApiResponse.success(categoryId);
    }

    @PutMapping
    @Operation(summary = "更新图书分类", description = "更新图书分类信息")
    public ApiResponse<Boolean> updateCategory(@RequestBody @Validated BookCategoryDTO categoryDTO) {
        boolean result = bookCategoryService.updateCategory(categoryDTO);
        return ApiResponse.success(result);
    }

    @DeleteMapping("/{categoryId}")
    @Operation(summary = "删除图书分类", description = "根据ID删除图书分类")
    public ApiResponse<Boolean> deleteCategory(@PathVariable Integer categoryId) {
        boolean result = bookCategoryService.deleteCategory(categoryId);
        return ApiResponse.success(result);
    }

    @GetMapping("/{categoryId}")
    @Operation(summary = "获取图书分类详情", description = "根据ID获取图书分类详情")
    public ApiResponse<BookCategoryDTO> getCategoryById(@PathVariable Integer categoryId) {
        BookCategoryDTO category = bookCategoryService.getCategoryById(categoryId);
        return ApiResponse.success(category);
    }

    @GetMapping
    @Operation(summary = "获取所有图书分类", description = "获取所有图书分类列表")
    public ApiResponse<List<BookCategoryDTO>> getAllCategories() {
        List<BookCategoryDTO> categories = bookCategoryService.getAllCategories();
        return ApiResponse.success(categories);
    }
}